package com.masterclient.app.businessmodule.homemodule.view.adapter.firstorcelains;

import android.content.Context;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridLayout;
import android.widget.ImageView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.LayoutHelper;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.eoner.baselibrary.utils.GlideUtil;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.eoner.commonbean.banner.BannerNewItemBean;
import com.masterclient.app.R;
import com.masterclient.app.businessmodule.homemodule.old.EventAdDataCallback;
import com.masterclient.app.businessmodule.homemodule.view.holder.HomeViewHolder;
import com.masterclient.app.utils.CommonUtil;

import java.util.List;

import static com.eoner.baselibrary.utils.ScreenUtils.dp2px;


public class HomePageXAdThreeAdapter extends DelegateAdapter.Adapter<HomeViewHolder> {

    private Context mContext;
    private LayoutHelper mLayoutHelper;
    private VirtualLayoutManager.LayoutParams mLayoutParams;
    private int mCount = 0;

    private List<BannerNewItemBean> shTemporaries;

    private EventAdDataCallback eventAdDataCallback;

    public void setEventAdDataCallback(EventAdDataCallback eventAdDataCallback) {
        this.eventAdDataCallback = eventAdDataCallback;
    }



    public HomePageXAdThreeAdapter(Context context, LayoutHelper layoutHelper, int count) {
        this(context, layoutHelper, count, null,null);
    }

    public HomePageXAdThreeAdapter(Context context, LayoutHelper layoutHelper, int count, @NonNull VirtualLayoutManager.LayoutParams layoutParams, List<BannerNewItemBean> shTemporaries) {
        this.mContext = context;
        this.mLayoutHelper = layoutHelper;
        this.mCount = count;
        this.mLayoutParams = layoutParams;
        this.shTemporaries = shTemporaries;
    }
    @Override
    public LayoutHelper onCreateLayoutHelper() {
        return mLayoutHelper;
    }
    @Override
    protected void onBindViewHolderWithOffset(HomeViewHolder holder, int position, int offsetTotal) {
    }

    @Override
    public HomeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return null;
    }
    @Override
    public void onBindViewHolder(HomeViewHolder holder, int position) {
        if(mLayoutParams!=null) {
            holder.itemView.setLayoutParams(
                    new VirtualLayoutManager.LayoutParams(mLayoutParams));
        }

        View adGridLayoutLayout = holder.itemView.findViewById(R.id.adGridLayoutLayout);
        GridLayout adGridLayout = holder.itemView.findViewById(R.id.adGridLayout);
        adGridLayout.setColumnCount(3);

        if(shTemporaries != null && shTemporaries.size() > 0){
            adGridLayoutLayout.setVisibility(View.VISIBLE);
            adGridLayout.removeAllViews();
                for (int i = 0; i < shTemporaries.size(); i++) {
                    BannerNewItemBean shTemporary = shTemporaries.get(i);

                int width = (ScreenUtils.getScreenWidth() - dp2px(32))/ 3;
                int height = width * 141 /112;
                if (!TextUtils.isEmpty(shTemporary.getWidth()) && !TextUtils.isEmpty(shTemporary.getHeight())){
                    height = width * Integer.parseInt(shTemporary.getHeight()) /Integer.parseInt(shTemporary.getWidth());
                }
                GridLayout.LayoutParams params = new GridLayout.LayoutParams(new ViewGroup.LayoutParams(width, height));
                View view = View.inflate(mContext,R.layout.gridlayout_ad_item,null);
                view.setTag(i);
                ImageView imageView = view.findViewById(R.id.image);
                GlideUtil.displayImage(mContext,shTemporary.getImage(),imageView);
                view.setLayoutParams(params);
                adGridLayout.addView(view);
                view.setOnClickListener(v->{
                    eventAdDataCallback.adData(shTemporary.getS_ad_name(),shTemporary.getS_ad_id(),shTemporary.getS_group_name(),shTemporary.getS_group_id(),
                            shTemporary.getTarget(), shTemporary.getTarget_id(),((int)view.getTag())+"");
                    CommonUtil.goNewAnyWhere(mContext,shTemporary.getTarget_platform(),shTemporary.getTarget_message(), shTemporary.getTarget(), shTemporary.getTarget_id(), shTemporary.getImage(), shTemporary.getS_ad_name(), shTemporary.getS_ad_id());
                });
            }
        }else{
            adGridLayoutLayout.setVisibility(View.GONE);
        }

    }

    @Override
    public int getItemCount() {
        return mCount;
    }

    @Override
    public int getItemViewType(int position) {
        return 14;
    }
}
